/**************************************************************
  > File Name: 135.c
  > Author: LuoLiang
  > Mail: 1204553475@qq.com 
  > Created Time: 2022年02月07日 星期一 14时25分38秒
 **************************************************************/

#include <stdio.h>
#include <stdlib.h>

int candy(int* ratings, int ratingsSize)
{
	int i, sum = 0;
	int *candies = (int *)calloc(ratingsSize,sizeof(int));

	for (i = 0; i < ratingsSize; i++)
	{
		candies[i] = 1;
	}

	for (i = 0; i < ratingsSize - 1; i++)
	{
		if (ratings[i+1] > ratings[i])
		{
			candies[i+1] = candies[i] + 1;
		}
	}
	for (i = ratingsSize - 1; i > 0; i--)
	{
		if (ratings[i-1] > ratings[i] && candies[i-1] <= candies[i])
		{
			candies[i-1] = candies[i] + 1;
		}
	}
	
	for(i = 0; i < ratingsSize; i++)
	{
		sum += candies[i];
	}
	return sum;
}

int main(int argc,char **argv)
{
	int ret;
	int arr[] = {1,0,2,3,4};

	ret = candy(arr,5);
	printf("%d\n",ret);
    exit(0);
}
